<?php

/*
 * @requires inc/005.autoload-function.php 0.3.1
 * @version 0.1
 * @package 0.3.1
 */

class Styler {

    private static $conf;
    private static $db_used = false;
    private static $skincache;

    static function init() {
        if (! self::$conf)
            self::$conf = new Config ( 'skin' );
    }

    private static function skin_dir($skin = false) {
        if (! $skin)
            $skin = self::$conf->default;
        return 'usr/skins/' . $skin;
    }

    public static function locate($file) {
        Log::put ( 'Styler searching for file ' . $file, 'styler.log' );
        preg_match ( '/\\.([^\\.]+)$/', $file, $ext );
        $ext = $ext [1];

        $so = self::get_vector();
        for($x = 0; $x < count ( $so ); $x ++) {
            $dir = self::skin_dir ( $so [$x] );
            $test = $dir . '/' . $ext . '/' . $file;
            if (file_exists ( $test )) {
                Log::put ( "File `$file` located as `$test`", 'styler.log' );
                return $test;
            }
        }

        Log::warning ( 'Styler was unable to locate file <' . $file . '>!', 'styler.log' );
        return '';
    }

    private static function build_skin_css($skin = NULL) {
        if ($skin === NULL)
            $skin = self::$conf->default;
        $ret = "\r\n\r\n/* =================================================================\r\n" . "   CSS code of skin <$skin>\r\n" . "   ================================================================= */\r\n";
        $dir = self::skin_dir ( $skin ) . '/css';
        if ($incdir = opendir ( $dir )) {
            while ( false !== ($file = readdir ( $incdir )) ) {
                if (is_file ( $dir . '/' . $file )) {
                    $ret .= "\r\n /* ---------------------------------------\r\n$file contents\r\n*/\r\n" . file_get_contents ( $dir . '/' . $file );
                }
            }
        }
        return $ret;
    }

    public static function build_css() {
        $so = self::get_vector();
        $ret = '';
        for($x = count ( $so ); $x > 0; $x --) {
            $ret .= self::build_skin_css ( $so [$x - 1] );
        }

        return $ret;
    }

    private static function get_user_skins() {
        $skins = array ();
        if (self::$db_used)
            return $skins;
        Log::put ( 'loading user skins...', 'styler.log' );
        try {
            $res = DataSource::select ( 'group, $skin.', '', 'skin.weight ASC' );
        }
        catch ( Exception $e ) {
            self::$db_used = true;
            Log::warning ( 'FAILED', 'styler.log' );
            return $skins;
        }

        if ($res) {
            while ( $line = $res->current () ) {
                $skins [] = $line->skin->name;
                $res->next ();
            }
            Log::put ( 'Done, loaded ' . count ( $skins ) . ' skins', 'styler.log' );
        }
        else Log::put('Loading failed: no result', 'styler.log');
        return $skins;
    }

    public static function get_vector() {
        if (!self::$skincache) {
            $so = self::$conf->skin_order;
            $uo = self::get_user_skins ();
            self::$skincache = array_merge ( $uo, $so );
        }
        return self::$skincache;
    }

    public static function search($pattern) {
        Log::put ( 'Styler searching for files that matching ' . $pattern, 'styler.log' );

        $so = array_reverse(self::get_vector());
        $files = array();
        for($x = 0; $x < count ( $so ); $x ++) {
            $dir = self::skin_dir ( $so [$x] );
            if (($idir = opendir($dir)) != false) {
                while(false !== ($edir = readdir($idir))) {
                    if (in_array($edir, array('.', '..'))) continue;
                    $edir = $dir.'/'.$edir;
                    if (($iedir = opendir($edir)) != false) {
                        while(false !== ($file = readdir($iedir))) {
                            if (in_array($file, array('.', '..'))) continue;
                            if (preg_match($pattern, $file)) {
                                $files[$file] = $edir.'/'.$file;
                            }
                        }
                    }
                    $edir = false;
                }
            }
        }

        Log::warning ( 'Found '.count($files).' files.', 'styler.log' );
        return $files;
    }
}

?>